import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:ddlmanager/backend/ddl.dart';
import 'package:ddlmanager/backend/app_state.dart';
import 'package:ddlmanager/utils/sort_drop_down.dart';

class CompletedDDLPage extends StatefulWidget {
  const CompletedDDLPage({super.key});

  @override
  State<CompletedDDLPage> createState() => _CompletedDDLPageState();
}

class _CompletedDDLPageState extends State<CompletedDDLPage> {
  late List<DDL> ddlList;
  late dynamic appState;

  // DDL详情对话框方法
  void showDDLDetailDialog(BuildContext context, DDL ddl) {
    final size = MediaQuery.of(context).size;
    final isMobile = size.width < 600;
    showDialog(
      context: context,
      builder:
          (context) => AlertDialog(
            title: Text(ddl.title),
            content: Container(
              width: isMobile ? size.width * 0.80 : 400, // 移动端占80%宽度
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 截止日期
                  ListTile(
                    leading: Icon(Icons.calendar_today),
                    title: Text('截止日期'),
                    subtitle: Text(ddl.deadline.toString().split(' ')[0]),
                  ),
                  ListTile(
                    leading: Icon(Icons.access_time),
                    title: Text('完成时间'),
                    subtitle: Text(ddl.completionDate.toString().split('.')[0]),
                  ),

                  // 优先级
                  ListTile(
                    leading: Icon(Icons.flag),
                    title: Text('优先级'),
                    subtitle: Text(
                      ddl.priority == 1
                          ? '高'
                          : ddl.priority == 2
                          ? '中'
                          : ddl.priority == 3
                          ? '低'
                          : '未知',
                    ),
                  ),
                  ListTile(
                    leading: Icon(Icons.star_outline_rounded),
                    title: Text('状态'),
                    subtitle: Text(ddl.isCompleted ? '已完成' : '未完成'),
                  ),
                ],
              ),
            ),
            contentPadding: EdgeInsets.symmetric(
              horizontal: isMobile ? 16 : 24, // 移动端更小的边距
              vertical: 20,
            ),
            actions: [
              TextButton(
                child: Text('确认'),
                onPressed: () => Navigator.pop(context),
              ),
            ],
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    appState = context.watch<AppState>();
    ddlList = appState.ddlListCompleted.ddlList;

    return Scaffold(
      appBar: AppBar(title: Text('这是你已完成的DDL')),
      body: Stack(
        children: [
          Column(
            children: [
              ListTile(
                title: Text('排序: '),
                trailing: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    // 使用排序下拉菜单
                    SortDropdown(
                      initialSort: SortOption.title,
                      onSortChanged: (SortOption option) {
                        // 这里可以处理排序逻辑
                        switch (option) {
                          case SortOption.deadline:
                            appState.sortByDeadline(false);
                            break;
                          case SortOption.priority:
                            appState.sortByPriority(false);
                            break;
                          case SortOption.title:
                            appState.sortByTitle(false);
                            break;
                          case SortOption.updateTime:
                            appState.sortByUpdateTime(false);
                            break;
                        }
                      },
                    ),
                  ],
                ),
              ),
              Expanded(
                child: ListView.builder(
                  itemCount: ddlList.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(ddlList[index].title),
                      subtitle: Text(
                        '完成日期: ${ddlList[index].completionDate.toString().split('.')[0]}',
                      ),
                      trailing: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Tooltip(
                            message: '还原',
                            child: IconButton(
                              icon: Icon(Icons.undo),
                              onPressed: () {
                                appState.restoreCompletedDDL(index);
                              },
                            ),
                          ),
                          Tooltip(
                            message: '删除',
                            child: IconButton(
                              icon: Icon(Icons.delete),
                              onPressed: () {
                                appState.deleteCompletedDDL(index);
                              },
                            ),
                          ),
                        ],
                      ),
                      onTap:
                          () => showDDLDetailDialog(
                            context,
                            ddlList[index],
                          ), // 直接传入DDL对象
                    );
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
